package org.example.domain.strategy.service.rule.chain.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.domain.strategy.repository.IStrategyRuleRepository;
import org.example.domain.strategy.service.rule.chain.AbstractLogicChain;
import org.example.types.common.Constants;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component("rule_blacklist")
public class BackListLogicChain extends AbstractLogicChain {
    @Resource
    IStrategyRuleRepository strategyRuleRepository;
    @Override
    public String ruleModel() {
        return "rule_blacklist";
    }

    @Override
    public Long logic(Long userId, Long strategyId) {
        log.info("正进行权重前置规则过滤。。。。userId：{}  strategyId：{}", userId, strategyId);
        //查询黑名单
        String ruleValue = strategyRuleRepository.queryRuleValue(strategyId, null, ruleModel());
        if (null != ruleValue) {
            String[] ruleFormat = ruleValue.split(Constants.COLON);
            Long awardId = Long.valueOf(ruleFormat[0]);
            String userList = ruleFormat[1];
            List<Long> userListFormat = Arrays.stream(userList.split(Constants.SPLIT)).map(Long::valueOf).toList();
            if (userListFormat.contains(userId)) {
                //黑名单用户直接返回
                log.info("权重前置规则过滤返回。。。。awardId：{}", awardId);
                return awardId;
            }
        }
        return next().logic(userId, strategyId);
    }
}
